#include "USART.h"

COM4_TypeDef COM4 = {0};
COM0_TypeDef COM0 = {0};

/*
 *@brief:
 *@parameter:
 *@ReturnVlaue:
 *@note:
 *@data:
 *@author:
 */
void USART0_InitConfig(uint32_t bps)
{
    FL_GPIO_InitTypeDef GPIO_InitStruct;
    FL_UART_InitTypeDef defaultInitStruct;

    GPIO_InitStruct.pin = FL_GPIO_PIN_13;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_DISABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;

    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.pin = FL_GPIO_PIN_14;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_DISABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;

    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    defaultInitStruct.clockSrc = FL_RCC_UART0_CLK_SOURCE_APB1CLK;
    defaultInitStruct.baudRate = bps;
    defaultInitStruct.dataWidth = FL_UART_DATA_WIDTH_8B;
    defaultInitStruct.stopBits = FL_UART_STOP_BIT_WIDTH_1B;
    defaultInitStruct.parity = FL_UART_PARITY_NONE;
    defaultInitStruct.transferDirection = FL_UART_DIRECTION_TX_RX;

    FL_UART_Init(UART0, &defaultInitStruct);   
}

/*
 *@brief:
 *@parameter:
 *@ReturnVlaue:
 *@note:
 *@data:
 *@author:
 */
void USART0_ReceiveIntertupt_InitConfig(void)
{
    FL_NVIC_ConfigTypeDef InterruptConfigStruct;
    
    InterruptConfigStruct.preemptPriority = 0x02;
    FL_NVIC_Init(&InterruptConfigStruct, UART0_IRQn);

    FL_UART_ClearFlag_RXBuffFull(UART0);
    FL_UART_EnableIT_RXBuffFull(UART0);
}

/*
 *@brief:
 *@parameter:
 *@ReturnVlaue:
 *@note:
 *@data:
 *@author:
 */
void UART0_IRQHandler(void)
{
	uint8_t tempbyte;
	
	if((FL_ENABLE == FL_UART_IsEnabledIT_RXBuffFull(UART0))&&(FL_SET == FL_UART_IsActiveFlag_RXBuffFull(UART0)))
	{
		tempbyte = FL_UART_ReadRXBuff(UART0);
        if(((COM0.RxBuffTail + 1) % USART0_BUFFMAXSIZE) != COM0.RxBuffHead)
        {
            COM0.RxBuff[COM0.RxBuffTail] = tempbyte;
            COM0.RxBuffTail = (COM0.RxBuffTail + 1) % USART4_BUFFMAXSIZE;
        }
		FL_UART_WriteTXBuff(UART4, tempbyte);
	}
}

/*
 *@brief:
 *@parameter:
 *@ReturnVlaue:
 *@note:
 *@data:
 *@author:
 */
void USART4_InitConfig(uint32_t bps)
{
    FL_GPIO_InitTypeDef GPIO_InitStruct;
    FL_UART_InitTypeDef defaultInitStruct;

    GPIO_InitStruct.pin = FL_GPIO_PIN_0;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_DISABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;

    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.pin = FL_GPIO_PIN_1;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_DISABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;

    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    defaultInitStruct.clockSrc = FL_RCC_UART0_CLK_SOURCE_APB1CLK;
    defaultInitStruct.baudRate = bps;
    defaultInitStruct.dataWidth = FL_UART_DATA_WIDTH_8B;
    defaultInitStruct.stopBits = FL_UART_STOP_BIT_WIDTH_1B;
    defaultInitStruct.parity = FL_UART_PARITY_NONE;
    defaultInitStruct.transferDirection = FL_UART_DIRECTION_TX_RX;

    FL_UART_Init(UART4, &defaultInitStruct);    
}

/*
 *@brief:
 *@parameter:
 *@ReturnVlaue:
 *@note:
 *@data:
 *@author:
 */
void USART4_UseDMAChannel3SendData_Init(void)
{
    FL_DMA_InitTypeDef defaultInitStruct;

    defaultInitStruct.periphAddress = FL_DMA_PERIPHERAL_FUNCTION4;
    defaultInitStruct.direction = FL_DMA_DIR_RAM_TO_PERIPHERAL;
    defaultInitStruct.memoryAddressIncMode = FL_DMA_MEMORY_INC_MODE_INCREASE;
    defaultInitStruct.flashAddressIncMode = FL_DMA_CH7_FLASH_INC_MODE_INCREASE;
    defaultInitStruct.dataSize = FL_DMA_BANDWIDTH_8B;
    defaultInitStruct.priority = FL_DMA_PRIORITY_HIGH;
    defaultInitStruct.circMode = FL_DISABLE;

    FL_DMA_Init(DMA,&defaultInitStruct, FL_DMA_CHANNEL_3);
}

/*
 *@brief:
 *@parameter:
 *@ReturnVlaue:
 *@note:
 *@data:
 *@author:
 */
void USART4_UseDMAChannel3SendData(uint8_t *buffer, uint32_t length)
{
    FL_DMA_ConfigTypeDef DMA_ConfigStruct={0}; 
    DMA_ConfigStruct.memoryAddress = (uint32_t)buffer;
    DMA_ConfigStruct.transmissionCount = length - 1;
    FL_DMA_StartTransmission(DMA, &DMA_ConfigStruct, FL_DMA_CHANNEL_3);    
}

/*
 *@brief:
 *@parameter:
 *@ReturnVlaue:
 *@note:
 *@data:
 *@author:
 */
void USART4_ReceiveIntertupt_InitConfig(void)
{
    FL_NVIC_ConfigTypeDef InterruptConfigStruct;
    
    InterruptConfigStruct.preemptPriority = 0x02;
    FL_NVIC_Init(&InterruptConfigStruct, UART4_IRQn);

    FL_UART_ClearFlag_RXBuffFull(UART4);
    FL_UART_EnableIT_RXBuffFull(UART4);
}

/*
 *@brief:
 *@parameter:
 *@ReturnVlaue:
 *@note:
 *@data:
 *@author:
 */
void UART4_IRQHandler(void)
{
	uint8_t tempbyte;
	
	if((FL_ENABLE == FL_UART_IsEnabledIT_RXBuffFull(UART4))&&(FL_SET == FL_UART_IsActiveFlag_RXBuffFull(UART4)))
	{
		tempbyte = FL_UART_ReadRXBuff(UART4);
        if(((COM4.RxBuffTail + 1) % USART4_BUFFMAXSIZE) != COM4.RxBuffHead)
        {
            COM4.RxBuff[COM4.RxBuffTail] = tempbyte;
            COM4.RxBuffTail = (COM4.RxBuffTail + 1) % USART4_BUFFMAXSIZE;
        }
	}
}
